/*
 * Copyright 2011-2012 Redia A/S
 * 
 * This file is part of Redias Library App for Android.
 *
 *  Redia Library App for Android is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Redia Library App for Android is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *  
 *  You should have received a copy of the GNU General Public License
 *  along with Redia Library App for Android.  If not, see <http://www.gnu.org/licenses/>.
 */
package dk.redia.libraryapp.model;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.R.string;

import dk.redia.lib.utils.AppConfiguration;
import dk.redia.lib.xmlrpc.XmlRpcClientResultListener;
import dk.redia.lib.xmlrpc.XmlRpcResultDumper;
import dk.redia.lib.xmlrpc.XmlRpcResultUtils;
import dk.redia.libraryapp.xmlrpc.library.XmlRpcClient;
import dk.redia.lib.log.Log;

public class ReservationBranchModel {

	private static final String logTag = "ReservationBranchModel";
	
	private static ReservationBranchModel instance;
	private List<ReservationBranch> reservationBranches;
	
	public static ReservationBranchModel getInstance() {
		if (instance==null) {
			instance = new ReservationBranchModel();
		}
		return instance;
	}
	
	private boolean reservationBranchCacheOk() {
		return false;
	}
	
	private ReservationBranchModel() {
		reservationBranches = new ArrayList<ReservationBranch>();
	}
	
	public void getReservationBranches(final ReservationBranchModelListener l) {
		if (reservationBranchCacheOk()) {
			
		} else {
			XmlRpcClient.getInstance().getReservationBranches(
					AppConfiguration.getInstance().getCustomerId(), 
					AppConfiguration.getInstance().getApiKey(), 
					new XmlRpcClientResultListener() {
				
				public void callFailed(String message) {
					l.getReservationsFailure(message);
				}
				
				public void callCompleted(Object resultObj) {
					XmlRpcResultDumper.dump(resultObj);
					Map<String, Object> result = XmlRpcResultUtils.unpackStandardResult(resultObj);
					if (result == null || result.containsKey("data") == false) {
						String message = "Invalid XML-RPC result data received.";
						Log.w(ReservationBranchModel.logTag, message);
						l.getReservationsFailure(message);
						return;
					}
					Map<String, Object> resultData = XmlRpcResultUtils.unpackStruct(result.get("data"));
					if (resultData == null || resultData.containsKey("branches") == false) {
						String message = "XML-RPC result data malformed or missing.";
						Log.w(ReservationBranchModel.logTag, message);
						l.getReservationsFailure(message);
						return;
					}
					Map<String, Object> branches = XmlRpcResultUtils.unpackStruct(resultData.get("branches"));
					if (branches == null) {
						String message = "XML-RPC result data malformed.";
						Log.w(ReservationBranchModel.logTag, message);
						l.getReservationsFailure(message);
						return;
					}
					List<ReservationBranch> rbs = new ArrayList<ReservationBranch>();
					for (String key : branches.keySet()) {
						ReservationBranch rb = new ReservationBranch();
						rb.setId(key);
						rb.setName((String)branches.get(key));
						rbs.add(rb);
					}
					reservationBranches = rbs;
					l.getReservationBranchesResult(reservationBranches);
				}
			});
		}
	}
	
}
